﻿namespace S7.Net
{
    using S7.Net.Types;
    using System;
    using System.Globalization;
    using System.Runtime.CompilerServices;

    public static class Conversion
    {
        public static byte? BinStringToByte(this string txt)
        {
            if (txt.Length == 8)
            {
                return new byte?((byte) txt.BinStringToInt32());
            }
            return null;
        }

        public static int BinStringToInt32(this string txt)
        {
            int num = 0;
            for (int i = 0; i < txt.Length; i++)
            {
                num = (num << 1) | ((txt[i] == '1') ? 1 : 0);
            }
            return num;
        }

        public static double ConvertToDouble(this uint input)
        {
            return S7.Net.Types.Double.FromByteArray(DWord.ToByteArray(input));
        }

        public static int ConvertToInt(this uint input)
        {
            return int.Parse(input.ToString("X"), NumberStyles.HexNumber);
        }

        public static short ConvertToShort(this ushort input)
        {
            return short.Parse(input.ToString("X"), NumberStyles.HexNumber);
        }

        public static uint ConvertToUInt(this double input)
        {
            return DWord.FromByteArray(S7.Net.Types.Double.ToByteArray(input));
        }

        public static uint ConvertToUInt(this int input)
        {
            return uint.Parse(input.ToString("X"), NumberStyles.HexNumber);
        }

        public static ushort ConvertToUshort(this short input)
        {
            return ushort.Parse(input.ToString("X"), NumberStyles.HexNumber);
        }

        public static bool SelectBit(this byte data, int bitPosition)
        {
            int num = ((int) 1) << bitPosition;
            return ((data & num) > 0);
        }

        public static string ValToBinString(this object value)
        {
            string name;
            int num = 0;
            int index = 0;
            int num3 = 0;
            string str = "";
            long num4 = 0;
            try
            {
                if (value.GetType().Name.IndexOf("[]") >= 0)
                {
                    goto Label_00F1;
                }
                name = value.GetType().Name;
                if (!(name == "Byte"))
                {
                    if (name != "Int16")
                    {
                        if (name == "Int32")
                        {
                            goto Label_008D;
                        }
                        if (name != "Int64")
                        {
                            throw new Exception();
                        }
                        goto Label_009B;
                    }
                }
                else
                {
                    num3 = 7;
                    num4 = (byte) value;
                    goto Label_00AE;
                }
                num3 = 15;
                num4 = (short) value;
                goto Label_00AE;
            Label_008D:
                num3 = 0x1f;
                num4 = (int) value;
                goto Label_00AE;
            Label_009B:
                num3 = 0x3f;
                num4 = (long) value;
            Label_00AE:
                num = num3;
                while (num >= 0)
                {
                    if ((num4 & ((long) Math.Pow(2.0, (double) num))) > 0)
                    {
                        str = str + "1";
                    }
                    else
                    {
                        str = str + "0";
                    }
                    num += -1;
                }
                goto Label_02C3;
            Label_00F1:
                name = value.GetType().Name;
                if (!(name == "Byte[]"))
                {
                    if (name != "Int16[]")
                    {
                        if (name == "Int32[]")
                        {
                            goto Label_0203;
                        }
                        if (name != "Int64[]")
                        {
                            throw new Exception();
                        }
                        goto Label_0260;
                    }
                }
                else
                {
                    num3 = 7;
                    byte[] buffer = (byte[]) value;
                    for (index = 0; index <= (buffer.Length - 1); index++)
                    {
                        num = num3;
                        while (num >= 0)
                        {
                            if ((buffer[index] & ((byte) Math.Pow(2.0, (double) num))) > 0)
                            {
                                str = str + "1";
                            }
                            else
                            {
                                str = str + "0";
                            }
                            num += -1;
                        }
                    }
                    goto Label_02C3;
                }
                num3 = 15;
                short[] numArray = (short[]) value;
                for (index = 0; index <= (numArray.Length - 1); index++)
                {
                    num = num3;
                    while (num >= 0)
                    {
                        if ((numArray[index] & ((byte) Math.Pow(2.0, (double) num))) > 0)
                        {
                            str = str + "1";
                        }
                        else
                        {
                            str = str + "0";
                        }
                        num += -1;
                    }
                }
                goto Label_02C3;
            Label_0203:
                num3 = 0x1f;
                int[] numArray2 = (int[]) value;
                for (index = 0; index <= (numArray2.Length - 1); index++)
                {
                    num = num3;
                    while (num >= 0)
                    {
                        if ((numArray2[index] & ((byte) Math.Pow(2.0, (double) num))) > 0)
                        {
                            str = str + "1";
                        }
                        else
                        {
                            str = str + "0";
                        }
                        num += -1;
                    }
                }
                goto Label_02C3;
            Label_0260:
                num3 = 0x3f;
                byte[] buffer2 = (byte[]) value;
                for (index = 0; index <= (buffer2.Length - 1); index++)
                {
                    for (num = num3; num >= 0; num += -1)
                    {
                        if ((buffer2[index] & ((byte) Math.Pow(2.0, (double) num))) > 0)
                        {
                            str = str + "1";
                        }
                        else
                        {
                            str = str + "0";
                        }
                    }
                }
            Label_02C3:
                name = str;
            }
            catch
            {
                name = "";
            }
            return name;
        }
    }
}

